package uf.tax.laws.spider.gs;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Properties;

import uf.tax.laws.spider.Dicter;
import uf.tax.laws.spider.HttpDAO;
import uf.tax.laws.spider.Utils;

public class App {

	static Properties config;
	private String base_dir;
	private int fileCount;
	private HttpDAO dao;

	public App() {
		dao = new HttpDAO();
		dao.setEncoding("utf-8");
		dao.get("http://www.chinatax.gov.cn/"); // get session
		dao.get("http://hd.chinatax.gov.cn/guoshui/main.jsp");
	}

	public void start() {
		List<String> types = new GetTypes(dao).getTypes();
		String typeConfig = base_dir + File.separator + "type.dat";
		String datConfig = base_dir + File.separator + "data.dat";
		Dicter dataDicter = new Dicter(datConfig);
		Dicter typeDicter = new Dicter(typeConfig);
		int uriCount = 0;
		for (String type : types) {
			if (typeDicter.getProperty(type, "false").equals("true")) {
				System.out.println(String.format("=== %s exists ===", type));
				continue;
			}
			File dir = new File(base_dir + File.separator + type);
			if (!dir.exists()) {
				dir.mkdirs();
			}
			List<String> urls = new GetLawList(dao, type).getLawList();
			for (String url : urls) {
				if (dataDicter.getProperty(url, "false").equals("true")) {
					System.out.println(String.format("%s exists", url));
					continue;
				}
				GetLawItem gli = new GetLawItem(dao, url);
				String title = gli.getTitle();
				int retryCount = 0;
				if (title == null) {
					System.out.println(String.format("retry url: %s [%d]", url, (retryCount + 1)));
					if (retryCount > 5)
						continue;
					while (title == null) {
						gli = new GetLawItem(dao, url);
						title = gli.getTitle();
						retryCount++;
						try {
							Thread.sleep(500);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				}

				if (title.length() > 120) {
					title = title.substring(0, 120);
				}
				String content = gli.getContent();
				try {
					Utils.strToFile(content, base_dir + File.separator + type + File.separator + title + ".html",
							"UTF-8");
					dataDicter.put(url, "true");
					uriCount++;
					if (uriCount % 100 == 0)
						dataDicter.save();
					System.out.println(String.format("%s %s saved", type, title));
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			typeDicter.put(type, "true");
			typeDicter.save();
		}
		dataDicter.save();
	}

	public static void main(String[] args) {
		if (args.length != 1) {
			System.out.println("Usage: App properties-file\nProperties format:\nstore_dir=/law/to/store/directory");
			return;
		}

		config = Utils.getConfig(args[0]);

		System.out.println("start...");
		App app = new App();
		app.base_dir = config.getProperty("store_dir", null);
		System.out.println("base dir: " + app.base_dir);
		if (app.base_dir == null) {
			System.out.println("store dir is invalid");
			System.exit(1);
		}
		app.start();
		System.out.println("processed! " + app.fileCount);
	}
}
